﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Cryptophone2
{ 
    class MySocket
    {
        [DllImport("MySocketDLL.dll", EntryPoint = "sock_crea", CharSet = CharSet.Auto)]
        private static extern int sock_crea([MarshalAs(UnmanagedType.LPWStr)] StringBuilder ipNumber, 
                                                                              int numero,
                                            [MarshalAs(UnmanagedType.LPWStr)] StringBuilder returnIp);

        [DllImport("MySocketDLL.dll", EntryPoint = "sock_chiudi")]
        private static extern void sock_chiudi();


        [DllImport("MySocketDLL.dll", EntryPoint = "sock_scambioChiavi", CharSet = CharSet.Auto)]
        private static extern int sock_scambioChiavi(   [MarshalAs(UnmanagedType.LPWStr)] StringBuilder keyTX,
                                                        [MarshalAs(UnmanagedType.LPWStr)] StringBuilder keyRX,
                                                        [MarshalAs(UnmanagedType.LPWStr)] StringBuilder keyVocal);

        /// <summary>
        /// Wrapping for DLL function sock_crea(). 
        /// This function initializes sockets for communications
        /// </summary>
        /// <param name="ipNumber">Ip of server (used only by client)</param>
        /// <param name="numero">Direction of connection 
        /// 0 = ServerSide, 1 = ClientSide</param>
        /// <param name="returnIp">Output Parameter. 
        /// Used in ServerSide mode to returning ip fo Client</param>
        /// <returns>if return 0, communication is setted and ready to use. </returns>
        public static int CreaSocket(StringBuilder ipNumber, int numero, StringBuilder returnIp)
        {
                int result = sock_crea(ipNumber, numero, returnIp);
                return result;
        }

        /// <summary>
        /// Wrapping for DLL function sock_chiudi(). 
        /// This function closes all communication sockets
        /// </summary>
        public static void ChiudiSocket()
        {
            sock_chiudi();
        }

        /// <summary>
        /// Wrapping for DLL function sock_scambioChiavi(). 
        /// This function is the core of security.
        /// It's used to create and exchange public and private key. 
        /// It's generate also SAS keyString
        /// to avoid man in the middle attack
        /// </summary>
        /// <param name="keyTX">output parameter. Transmitting key </param>
        /// <param name="keyRX">output parameter. Reciving key</param>
        /// <param name="keyVocal">output parameter. SAS key</param>
        /// <returns>return 0 for success</returns>
        public static int ScambioChiaviSocket(  StringBuilder keyTX, 
                                                StringBuilder keyRX, 
                                                StringBuilder keyVocal)
        {
            int result = sock_scambioChiavi(keyTX, keyRX, keyVocal);
            return result;
        }    
    }
}
